package com.litxuf.web.action.front;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.LabelValueBean;

import com.litxuf.FinalInformation;
import com.litxuf.logic.core.ManagerFactory;
import com.litxuf.logic.member.dao.model.School;
import com.litxuf.logic.member.manager.MemberManager;
import com.litxuf.web.action.admin.froms.SchoolInfoForm;

public class Front_MemberBasicInfoAction extends DispatchAction {

	Logger log = Logger.getLogger(this.getClass());

	MemberManager manager = ManagerFactory.getFactory().getMemberManager();


	/**
	 * 修改基本资料 made by litxuf May 26, 2010-1:47:28 PM
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward myinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		School info = (School) request.getSession().getAttribute(
				FinalInformation.UserFrontLogin);
 		MemberManager m=ManagerFactory.getFactory().getMemberManager();
		
		log.info(" --> " + info.getMemberName() + " 准备修改基本资料");
		SchoolInfoForm infoForm = (SchoolInfoForm) form;

		String area[] = { null, null, null };

		// 区域
		String areacode = info.getAreacode();
		log.info("::areacode=" + areacode);
		// 综合
		if (areacode == null || areacode.equals("ZZZ")) {
			area = null;
		} else {
			if (areacode != null && areacode.length() == 6) {
				area[0] = areacode.substring(0, 2);
				area[1] = areacode.substring(0, 4);
				area[2] = areacode.substring(0, 6);
			}
			if (areacode != null && areacode.length() == 4) {
				area[0] = areacode.substring(0, 2);
				area[1] = areacode.substring(0, 4);
				area[2] = null;
			}

			if (areacode != null && areacode.length() == 2) {
				area[0] = areacode.substring(0, 2);
				area[1] = null;
				area[2] = null;
			}
			try {
				BeanUtils.copyProperties(infoForm, info);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {

				e.printStackTrace();
			}
		}
		
		 
		request.setAttribute("History", info.getHistory());
		request.setAttribute("Introduction", info.getIntroduction());
		request.setAttribute("E_AREA", area);
		return mapping.findForward("myinfo");

	}

	 

	/**
	 * 修改基本资料 made by litxuf May 26, 2010-2:03:22 PM
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward updatemyinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		SchoolInfoForm f = (SchoolInfoForm) form;
		School sms = (School) request.getSession().getAttribute(
				FinalInformation.UserFrontLogin);
		MemberManager m=ManagerFactory.getFactory().getMemberManager();
		 
 
		boolean fs = m.updateMember(sms);

		if (fs) {
			request.setAttribute("RESULT", "更新成功");
			
			log.info(" --> " + sms.getMemberName() + "  修改基本资料 success");

		} else {
			log.info(" --> " + sms.getMemberName() + "  修改基本资料 fail");
			request.setAttribute("RESULT", "更新失败");
		}
		return mapping.findForward("result");

	}

	/**
	 * 历史沿革 or 简介 made in 2010-12-8下午02:06:40
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * 
	 */
	public ActionForward hisOrIntr(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		School sms = (School) request.getSession().getAttribute(
				FinalInformation.UserFrontLogin);
		log.info(" --> " + sms.getMemberName() + " 准备 历史沿革 or 简介  ");

		String type = request.getParameter("type");

		if ("HIS".equals(type)) {
			request.setAttribute("History", sms.getHistory());

			return mapping.findForward("History");
		} else {
			request.setAttribute("ShortIntroduction", sms
					.getShortIntroduction());
			request.setAttribute("Introduction", sms.getIntroduction());
			return mapping.findForward("Introduction");

		}

	}

	public ActionForward updatehisOrIntr(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		School sms = (School) request.getSession().getAttribute(
				FinalInformation.UserFrontLogin);
		String type = request.getParameter("type");

		if ("HIS".equals(type)) {
			String his = request.getParameter("History");

			sms.setHistory(his);

		} else {
			sms.setIntroduction(request.getParameter("introduction"));
			sms.setShortIntroduction(request.getParameter("shortintroduction"));
		}

		boolean ff = manager.updateMember(sms);
		if (ff) {
			request.setAttribute("RESULT", "修改成功");
			// 更新session中的 用户对象
			request.getSession().setAttribute(FinalInformation.UserFrontLogin,
					sms);
			log.info(" --> " + sms.getMemberName() + " 历史沿革 or 简介 success");
		} else {
			request.setAttribute("RESULT", "数据库异常 修改失败");
			log.info(" --> " + sms.getMemberName() + " 历史沿革 or 简介 ERROR");
		}

		return mapping.findForward("result");

	}

	public ActionForward mypwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		School sm = (School) request.getSession().getAttribute(
				FinalInformation.UserFrontLogin);
		
		
		log.info("-->" + sm.getMemberName() + " 修改密码");
		String oldpwd = request.getParameter("oldpwd");
		String newpwd = request.getParameter("newpwd");
		log.info("-->oldpwd:" + oldpwd);
		log.info("-->newpwd:" + newpwd);
		log.info("-->+mempwd" + sm.getMemberPwd());
		if (!oldpwd.equals(sm.getMemberPwd())) {
			request.setAttribute("RESULT", "旧密码错误");
			log.info(" --> " + sm.getMemberName() + " 旧密码错误");
			return mapping.findForward("mypwd");
		}

		sm.setMemberPwd(newpwd);

		if(manager.updateMember(sm)){
			
			request.setAttribute("RESULT", "修改密码成功");
			log.info(" --> " + sm.getMemberName() + " 修改密码 SUCCESS");
		}else{
			
			request.setAttribute("RESULT", "数据库异常 修改失败");
			log.info(" --> " + sm.getMemberName() + " 修改密码 ERROR");
		}



		return mapping.findForward("result");

	}

}
